<?php
class Rank{
	const KI = 32;

	public function query_score($stu)
	{
		$sql = "SELECT * FROM stu WHERE `id` = {$stu}";
		$query = new DBMysql();
		$info = $query -> fetch($sql);
		return $info['score'];
	}

	public function update_score($Ra,$stu)
	{
		$query = new DBMysql();
		$query->query("UPDATE `stu` SET `score` = {$Ra} WHERE `id` = {$stu}");
	}

	public function expect($Ra, $Rb)
	{
		$Ea = 1/(1+pow(10,($Rb-$Ra)/400));
		return $Ea;
	}

	public function calculate_score($Ra, $Ea, $num)
	{
		$Ra = $Ra + KI*($num - $Ea);
		return $Ra;
	}

	public function select_stu()
	{
		require('DBMysql.php');
		$stu1 = @$_POST['stu1'];
		$stu2 = @$_POST['stu2'];
		$victoryid = @$_POST['id'];
		return $this->get_score($stu1, $stu2, $victoryid);
	}

	public function get_score($stu1, $stu2, $victoryid)
	{
		//查询目前分数
		$Ra = $this->query_score($stu1);
		$Rb = $this->query_score($stu2);
		if($Ra > $Rb)
		{
			//求期望值
			$Ea = $this->expect($Ra, $Rb);
			$Eb = $this->expect($Rb, $Ra);

			$Ra = $this->calculate_score($Ra, $Ea,$victoryid);
			$Rb = $this->calculate_score($Rb, $Eb, 1-$victoryid);
			$Rab = array($Ra, $Rb);
			//更新分数
			$this->update_score($Ra,$stu1);
			$this->update_score($Rb,$stu2);

			return $Rab;
		}
		else
		{
			return FALSE;
		}
	}
}
$run  = new Rank();
$a = $run->select_stu();
var_dump($a);